/**
 * 数据处理
 * @author echo.
 * @version 1.5.0
 **/
const utils = {
	//去空格
	trim: function(value) {
		return value.replace(/(^\s*)|(\s*$)/g, "");
	},
	//内容替换
	replaceAll: function(text, repstr, newstr) {
		return text.replace(new RegExp(repstr, "gm"), newstr);
	},
	//格式化手机号码
	formatNumber: function(num) {
		return num.length === 11 ? num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') : num;
	},
	
	/********************             ***************************/
	
	
	//判断是否有效手机号
	isPoneAvailable:function(poneInput) {
		var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
		if (!myreg.test(poneInput)) {
			return false;
		} else {
			return true;
		}
	},
	//判断是否有小数点
	isDot:function(num) {
	    var result = (num.toString()).indexOf(".");
	    if(result != -1) {
	        return true;
	    } else {
	       	return false;
	    }
	},
	//获取可用时间
	formatTodayHalfHours: function(day,start, end) {
		// return new Promise((resolve, reject) => {
			// console.log({1:start,2:end})
			
			start =(day+' '+ start).replace(/-/g, "/");
			var startTime = new Date(start).getTime();
			end = (day+' '+ end).replace(/-/g, "/");
			var endTime = new Date(end).getTime();
			// console.log({1:start,2:end})
			// console.log({1:startTime,2:endTime})
			// console.log(startTime<=endTime)
			var dateList = [];
			while (startTime <= endTime) {
				var hour = new Date(startTime).getHours() < 10 ? '0' + new Date(startTime).getHours() : new Date(startTime).getHours();
				var minute = new Date(startTime).getMinutes() < 30 ? '00' : '30';
				dateList.unshift(hour + ':' + minute);
				startTime += 30 * 60 * 1000;
			}
			// console.log(dateList.reverse());
			return dateList.reverse()
			// this.regionArr = dateList.reverse();
		// 	resolve(this.regionArr)
		// })
	},
	expandFun: function() {
		Date.prototype.format = function(fmt) {
			var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
			};
			if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
			for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" +
					o[k]).substr(("" + o[k]).length)));
			return fmt;
		}
	},
	getTodayTime: function(listDay,changeDay) {
		if(!changeDay){	
			var myDate = new Date() //获取系统当前时间
		}else{
			var myDate = new Date(changeDay)
		}
		var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
		var day = myDate.getDate(); //获取当前日(1-31)
		var week = myDate.getDay();
		for (var i = 0; i <= 2; i++) {
			if(!changeDay){
				myDate = new Date() //获取系统当前时间
			}else{
				myDate = new Date(changeDay)
			}
			myDate.setDate(myDate.getDate() + i)
			month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
			day = myDate.getDate(); //获取当前日(1-31)
			week = myDate.getDay();
			listDay[i].day = month + '月' + day + '日';
			if (week == 0) {
				week = '日'
			}
			if (week == 1) {
				week = '一'
			}
			if (week == 2) {
				week = '二'
			}
			if (week == 3) {
				week = '三'
			}
			if (week == 4) {
				week = '四'
			}
			if (week == 5) {
				week = '五'
			}
			if (week == 6) {
				week = '六'
			}
			listDay[i].week = '星期' + week;
			listDay[i].all = myDate.format("yyyy-MM-dd");
		}
		return listDay
	},
	//获取当前时间
	getToday:function(num,changeDay){
		
		var arr=[];
		if(!changeDay){
			var today=new Date();
		}else{
			var today=new Date(changeDay);
		}
		
		
		var year=today.getFullYear();
		var month=today.getMonth()+1;
		var date=today.getDate();
		var hour=today.getHours();
		var Minute=today.getMinutes();
		var Second=today.getSeconds();
		if(num){
			num=Number(num);
			date=new Date(today.setDate(today.getDate() + num)).getDate()
		}
		
		arr.push(year,month,date,hour,Minute,Second);
		
		return arr
	},
	//计算时间差
	calcDate:function(date1, date2) {
		// console.log({1:date1,2:date2})
		var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数          
		var days = Math.floor(date3 / (24 * 3600 * 1000))
		var leave1 = date3 % (24 * 3600 * 1000)
		var hours = Math.floor(leave1 / (3600 * 1000))
		var leave2 = leave1 % (3600 * 1000)
		var minutes = Math.floor(leave2 / (60 * 1000))
		var leave3 = leave2 % (60 * 1000)
		var seconds = Math.round(leave3 / 1000)
		// console.log(" 相差 "+days+"天 "+hours+"小时 "+minutes+" 分钟"+seconds+" 秒")  
		//只需要小时与分钟
		return hours + ' ' + minutes
	},
	
	
	
	
	
	
	/*************************      end    ***************************/
	
	
	
	//金额格式化
	rmoney: function(money) {
		return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
			/\,$/, '').split('').reverse().join('');
	},
	compare:function(value1,value2){
		if (value1 < value2){
			return -1;
		}else if (value1 > value2){
			return 1;
		}else{
			return 0;
		}
	},
	//日期格式化
	formatDate: function(formatStr, fdate) {
		if (fdate) {
			if (~fdate.indexOf('.')) {
				fdate = fdate.substring(0, fdate.indexOf('.'));
			}
			fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/');
			var fTime, fStr = 'ymdhis';
			if (!formatStr)
				formatStr = "y-m-d h:i:s";
			if (fdate)
				fTime = new Date(fdate);
			else
				fTime = new Date();
			var month = fTime.getMonth() + 1;
			var day = fTime.getDate();
			var hours = fTime.getHours();
			var minu = fTime.getMinutes();
			var second = fTime.getSeconds();
			month = month < 10 ? '0' + month : month;
			day = day < 10 ? '0' + day : day;
			hours = hours < 10 ? ('0' + hours) : hours;
			minu = minu < 10 ? '0' + minu : minu;
			second = second < 10 ? '0' + second : second;
			var formatArr = [
				fTime.getFullYear().toString(),
				month.toString(),
				day.toString(),
				hours.toString(),
				minu.toString(),
				second.toString()
			]
			for (var i = 0; i < formatArr.length; i++) {
				formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
			}
			return formatStr;
		} else {
			return "";
		}
	},
	rgbToHex: function (r, g, b) {
		return "#" + utils.toHex(r) + utils.toHex(g) + utils.toHex(b)
	},
	toHex: function (n) {
		n = parseInt(n, 10);
		if (isNaN(n)) return "00";
		n = Math.max(0, Math.min(n, 255));
		return "0123456789ABCDEF".charAt((n - n % 16) / 16) +
			"0123456789ABCDEF".charAt(n % 16);
	},
	hexToRgb(hex) {
		let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
		return result ? {
			r: parseInt(result[1], 16),
			g: parseInt(result[2], 16),
			b: parseInt(result[3], 16)
		} : null;
	}
}

module.exports = {
	trim: utils.trim,
	replaceAll: utils.replaceAll,
	formatNumber: utils.formatNumber,
	
	isPoneAvailable:utils.isPoneAvailable,
	isDot:utils.isDot,
	formatTodayHalfHours:utils.formatTodayHalfHours,
	expandFun:utils.expandFun,
	getTodayTime:utils.getTodayTime,
	getToday:utils.getToday,
	calcDate:utils.calcDate,
	
	rmoney: utils.rmoney,
	formatDate: utils.formatDate,
	rgbToHex: utils.rgbToHex,
	hexToRgb: utils.hexToRgb,
	compare:utils.compare
}
